package com.kgc.ljp.util;
import java.sql.*;

public abstract class BaseDao {
    protected Connection conn = null;
    protected PreparedStatement ps = null;
    protected ResultSet rs = null;
    public static Connection getConnection(){
        String url = ConfigManager.insetence().getString("url");
        String driver = ConfigManager.insetence().getString("driver");
        String name = ConfigManager.insetence().getString("name");
        String pwd = ConfigManager.insetence().getString("pwd");
        Connection conn = null;
        try {
            Class.forName(driver);
            if (conn==null){
                conn = DriverManager.getConnection(url,name,pwd);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    public static void closeAll(PreparedStatement ps,ResultSet rs,Connection conn){
            try {
                if (ps!=null){
                ps.close();
                }
                if (rs!=null){
                    rs.close();
                }
                if (conn!=null){
                    conn.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
    }

    public BaseDao() {
    }

    public BaseDao(Connection conn, PreparedStatement ps, ResultSet rs) {

        this.conn = conn;
        this.ps = ps;
        this.rs = rs;
    }

    protected abstract int update(String sql, Object [] param);
    protected abstract ResultSet select(String sql,Object [] param);
}
